package com.mycjj.android.activity;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.BaseBean;
import com.mycjj.android.bean.GameConfigBaseBean;
import com.mycjj.android.bean.GameConfigBaseBean.ConfigBean.Config;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;
import com.mycjj.android.widget.WScratchView;

//刮刮卡
public class Score_GuaGuaCardActivity extends BaseActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_score_guagua_card);
		initHeader();
		init();
		initData();
	}

	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;
	private WScratchView scratchView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("刮刮卡");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Score_GuaGuaCardActivity.this.finish();
			}
		});
	}

	private TextView textView;
	private TextView mTv_count, mTv_retry_count;
	private ListView listview;
	private boolean isShow;
	private ImageView iv_begin;

	private void init() {
		textView = (TextView) findViewById(R.id.rubbler);
		scratchView = (WScratchView) findViewById(R.id.scratch_view);
		scratchView.setOnScratchCallback(new WScratchView.OnScratchCallback() {

			@Override
			public void onScratch(float percentage) {
				if (percentage > 50.0) {
					if (!isShow) {
						isShow = true;
						Toast.makeText(Score_GuaGuaCardActivity.this, result, Toast.LENGTH_LONG).show();
					}
				}
			}

		});
		iv_begin = (ImageView) findViewById(R.id.iv_begin);
		iv_begin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				doGuaGua();
			}
		});
		mTv_count = (TextView) findViewById(R.id.tv_count);
		mTv_count.setText(String.valueOf(dataManager.getPoints()));
		mTv_retry_count = (TextView) findViewById(R.id.tv_retry_count);

		listview = (ListView) findViewById(R.id.lv_config);
		adapter = new GameConfigAdapter(this);
		listview.setAdapter(adapter);
		// scratchTextView.reset(0xFFFFFFFF);

	}

	private void initData() {
		currentPoints = dataManager.getPoints();
		getGameConfig();

	}

	private List<Config> dataList;
	private int retryPoints;
	private int currentPoints;

	private void getGameConfig() {
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		params.put("game", "scratch");
		request.requestGameGetConfigWithDoneHandler("", params, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				GameConfigBaseBean baseBean = dataManager.getGameConfigBaseBean();
				if (baseBean != null) {
					if (baseBean.getCode() == 200) {
						String pointsStr = baseBean.getData().getPoints();
						try {
							retryPoints = Integer.parseInt(pointsStr);
							Log.e("mark", "retryPoints:" + retryPoints);
						} catch (Exception e) {
							retryPoints = 50;
							e.printStackTrace();
						}
						Log.e("mark", "currentPoints:" + currentPoints + ";retryPoints:" + retryPoints);
						mTv_retry_count.setText(pointsStr);
						dataList = baseBean.getData().getPrize();
						adapter.setList(dataList);
						if (currentPoints < retryPoints) {// 当前用户积分小于retry的积分
							scratchView.setScratchable(false);
						} else {
							scratchView.setScratchable(true);
						}
					} else {
						Toast.makeText(Score_GuaGuaCardActivity.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
	}

	private String result;

	private void doGuaGua() {
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("client_id", ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		request.requestGameScratchWithDoneHandler("", params, new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				BaseBean<Map> baseBean = (BaseBean<Map>) dataManager.getBaseBean();
				if (baseBean != null) {
					if (baseBean.getCode() == 200) {
						iv_begin.setVisibility(View.GONE);
						scratchView.setVisibility(View.VISIBLE);
						textView.setVisibility(View.VISIBLE);
						Object obj = baseBean.getData().get("level");
						if (obj != null) {
							int level = Integer.parseInt(obj.toString());
							int tryCount = 50;
							try {
								tryCount = Integer.parseInt(mTv_retry_count.getText().toString().trim());
							} catch (Exception e) {
								tryCount = 50;
							}
							int nowPoints = dataManager.getPoints() - tryCount;
							result = "";
							if (level == -1) {
								result = "谢谢参与";
								textView.setText(result);
							} else {
								int points = getPoints(level);
								nowPoints += points;
								result = getName(level);
								textView.setText(result);
							}
							dataManager.setPoints(nowPoints);
							mTv_count.setText(String.valueOf(dataManager.getPoints()));
						}
					} else {
						scratchView.setScratchable(false);
						textView.setBackgroundColor(getResources().getColor(R.color.black));
						Toast.makeText(Score_GuaGuaCardActivity.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
	}

	private int getPoints(int level) {
		if (dataList != null && dataList.size() > 0) {
			for (Config cfg : dataList) {
				if (cfg.getLevel() == level) {
					return cfg.getPoints();
				} else {
					continue;
				}
			}
		}
		return 0;
	}

	private String getName(int level) {
		if (dataList != null && dataList.size() > 0) {
			for (Config cfg : dataList) {
				if (cfg.getLevel() == level) {
					return cfg.getName();
				} else {
					continue;
				}
			}
		}
		return "";
	}

	private final static int WHAT = 0;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case WHAT:
				doGuaGua();
				break;
			}
		}
	};

	private GameConfigAdapter adapter;

	public class GameConfigAdapter extends TAdapter<Config> {

		public GameConfigAdapter(Context mContext) {
			super(mContext);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_game_scratch_view, null);
				holder = new ViewHolder();
				holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				holder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
				holder.tv_level = (TextView) convertView.findViewById(R.id.tv_level);
				holder.ll_level_item = convertView.findViewById(R.id.ll_level_item);
				holder.tv_comfort = convertView.findViewById(R.id.tv_comfort);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Config bean = mList.get(position);
			holder.tv_name.setText(bean.getName());
			holder.tv_level.setText(String.valueOf(bean.getLevel()));
			holder.tv_desc.setText("中奖名额:" + bean.getMax() + "个");
			return convertView;
		}

		class ViewHolder {
			View ll_level_item;
			View tv_comfort;
			TextView tv_name;
			TextView tv_desc;
			TextView tv_level;
		}

	}
}
